package com.jbl.ma.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.ma.entity.Repair;
import com.jbl.ma.entity.RepairDescribe;
import com.jbl.ma.mapper.RepairMapper;
import com.jbl.ma.service.RepairDescribeService;
import com.jbl.ma.service.RepairService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jbl.ma.vo.RepairQuery;
import com.jbl.ma.vo.RepairVo;
import com.jbl.te.entity.Teacher;
import com.jbl.te.entity.User;
import com.jbl.te.service.TeacherService;
import com.jbl.te.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {
    @Autowired
    private RepairDescribeService repairDescribeService;

    @Autowired
    private TeacherService teacherService;

    @Transactional
    @Override
    public List<RepairVo> pageQuery(Page<Repair> pageParam, RepairQuery repairQuery) {
        QueryWrapper<Repair> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_create");

        if (repairQuery == null) {
            this.page(pageParam, wrapper);
            return null;
        }
        Integer repairType = repairQuery.getRepairType();
        Integer repairStatus = repairQuery.getRepairStatus();
        Integer repairUser = repairQuery.getRepairUser();
        String key = repairQuery.getKey();



        if (!StringUtils.isEmpty(repairType)) {
            wrapper.eq("repair_type", repairType);
        }
        if (!StringUtils.isEmpty(repairStatus)) {
            wrapper.eq("repair_status", repairStatus);
        }
        if (!StringUtils.isEmpty(repairUser)) {
            if (repairUser.equals(1)) {
                wrapper.isNotNull("user_id");
            }
            if (repairUser.equals(0)) {
                wrapper.isNull("user_id");
            }
        }

        if (!StringUtils.isEmpty(key)) {
            wrapper.like("repair_id", key).or()
                    .like("user_id", key).or()
                    .like("gmt_create", key);
        }
        this.page(pageParam, wrapper);

        List<Repair> records = pageParam.getRecords();

        List<RepairVo> collect = getRepairVos(records);

        return collect;
    }

    private List<RepairVo> getRepairVos(List<Repair> records) {
        return records.stream().map(repair -> {
            RepairVo repairVo = new RepairVo();
            BeanUtils.copyProperties(repair, repairVo);
            // 描述
            RepairDescribe describe = repairDescribeService.getById(repair.getRepairType());
            repairVo.setRepairDescribe(describe.getRepairDescribe());
            // 老师信息
            Teacher teacher = teacherService.getById(repair.getTeacherNumber());
            repairVo.setTeacherName(teacher.getName());
            repairVo.setPhone(teacher.getPhone());
            // 维修人信息
            if (repair.getUserId() != null) {
                Teacher teacherUser = teacherService.getById(repair.getUserId());
                repairVo.setUserName(teacherUser.getName());
                repairVo.setUserPhone(teacherUser.getPhone());
            }
            return repairVo;
        }).collect(Collectors.toList());
    }

    @Override
    public boolean repair(String repairId) {
        if (repairId != null) {
            // 在维修表中添加userId
            //System.out.println("repairId=>" + repairId);
            Repair repair = this.getById(repairId);
            repair.setRepairStatus(1);
            boolean update = this.updateById(repair);
            return update;
        } else {
            return false;
        }
    }

    @Transactional
    @Override
    public List<String> repairBatch(List<String> repairIds) {
        List<String> collect = repairIds.stream().map(repairId -> {
            if (repairId != null) {
                // 在维修表中添加userId
                Repair repair = this.getById(repairId);
                repair.setRepairStatus(1);
                this.updateById(repair);
            }
            return repairId;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public boolean receive(String repairId, String userId) {
        if (repairId != null && userId != null) {
            // 在维修表中添加userId
            Repair repair = this.getById(repairId);
            repair.setUserId(userId);
            boolean update = this.updateById(repair);
            return update;
        } else {
            return false;
        }
    }

    @Transactional
    @Override
    public List<String> receiveBatch(List<String> repairIds, String userId) {
        //System.out.println("repairIds: " + repairIds + ", userId: " + userId);
        List<String> collect = repairIds.stream().map(repairId -> {
            if (repairId != null && userId != null) {
                // 在维修表中添加userId
                Repair repair = this.getById(repairId);
                repair.setUserId(userId);
                this.updateById(repair);
            }
            return repairId;
        }).collect(Collectors.toList());
        return collect;
    }

    @Transactional
    @Override
    public List<RepairVo> repairList() {
        List<Repair> dataList = this.list();

        List<RepairVo> collect = getRepairVos(dataList);

        return collect;
    }

}
